package com.aaa.train.service.impl;


import com.aaa.common.entity.*;
import com.aaa.train.dao.TbCourseDao;
import com.aaa.train.dao.TbTrainerDao;
import com.aaa.train.service.TbTrainerService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 培训班老师 服务实现类
 * </p>
 *
 * @author wang
 * @since 2020-07-29
 */
@Service
public class TbTrainerServiceImpl extends ServiceImpl<TbTrainerDao, TbTrainer> implements TbTrainerService {
    @Autowired
    private TbTrainerDao tbTrainerDao;


    /*
     * description: 培训师的查询
      * @param null
     * @return
     */
    @Override
    public LayUiTable selectTrainerAll(Integer page, Integer limit,String tbTrainerName) {
        PageHelper.startPage(page,limit);
        List<Map> mapList = tbTrainerDao.selectTrainerAll(tbTrainerName);
        PageInfo pageInfo = new PageInfo(mapList);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(MyConstants.OPERATION_SUCCESS_CODE);
        layUiTable.setMsg(MyConstants.OPERATION_SUCCESS_MESSAGE);
        layUiTable.setData(pageInfo.getList());
        layUiTable.setCount(new Long(pageInfo.getTotal()).intValue());
        return layUiTable;
    }
    /*
     * description: 培训师的新增
      * @param null
     * @return 
     */
    
    @Override
    public ResultData updateTrain(TbTrainer tbTrainer) {
       if (tbTrainer.getStatus()== null){
           tbTrainer.setStatus(1);
       }
        Integer insert = tbTrainerDao.updateById(tbTrainer);
        if (insert > 0){
            return new ResultData(true,"修改成功",null);
        }else
            return new ResultData(false,"修改失败",null);
    }

    /*
     * description: 根据ID查询
      * @param null
     * @return 
     */
    
    @Override
    public LayUiTable selectTrainById(TbTrainer tbTrainer) {
        TbTrainer selectById = tbTrainerDao.selectById(tbTrainer);
        LayUiTable layUiTable = new LayUiTable();
        layUiTable.setCode(0);
        layUiTable.setMsg("查询成功");
        layUiTable.setData(selectById);
        return layUiTable;
    }


    /*
     * description: 
      * @param null
     * @return 
     */
    
    @Override
    public ResultData insertTrain(TbTrainer tbTrainer) {
        Integer insert = tbTrainerDao.insert(tbTrainer);
        if (insert > 0){
            return new ResultData(true,"新增成功",null);
        }else{
            return new ResultData(false,"新增失败",null);
        }

}
    /*
     * description: 新增校验
      * @param null
     * @return
     */

    @Override
    public ResultData selectName(TbTrainer tbTrainer) {
        System.out.println(tbTrainer.getTbTrainerId()+"=============");
        System.out.println(tbTrainer.getTbTrainerName()+"+++++++++++++");
        if (tbTrainer.getTbTrainerId()!=null){
            Wrapper<TbTrainer> wrapper = new EntityWrapper<>();
            wrapper.ne("tb_trainer_id",tbTrainer.getTbTrainerId());
            wrapper.eq("tb_trainer_name",tbTrainer.getTbTrainerName());
            List<TbTrainer> tbTrainerList = tbTrainerDao.selectList(wrapper);
            if (tbTrainerList.size()>0){
                return new ResultData(false,"重复",null);
            }else{
                return new ResultData(true,"可以使用",null);
            }
        }else {
            Wrapper<TbTrainer> wrapper = new EntityWrapper<>();
            wrapper.eq("tb_trainer_name", tbTrainer.getTbTrainerName());
            List<TbTrainer> tbTrainerList = tbTrainerDao.selectList(wrapper);
            if (tbTrainerList.size() > 0) {
                return new ResultData(false, "重复", null);
            } else {
                return new ResultData(true, "可以使用", null);
            }
        }
    }
    /*
     * description: 理论删除
      * @param null
     * @return
     */

    @Override
    public ResultData delete(TbTrainer tbTrainer) {
        tbTrainer.setStatus(1);
        Integer integer = tbTrainerDao.updateById(tbTrainer);
        if (integer > 0){
            return new ResultData(true,"删除成功",null);
        }else{
            return new ResultData(false,"删除失败",null);
        }
    }

}

